package com.example.demo.service;

import com.example.demo.entity.pojo.Book;
import com.example.demo.entity.pojo.BookType;
import com.example.demo.entity.vo.BookTypeVo;
import com.example.demo.entity.vo.BookVo;
import com.example.demo.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BookService {
    @Autowired
    BookMapper bookMapper;

    /**
     * 获取所有书籍
     * @return
     */
    public List<BookVo> getAllBook(){
        List<BookVo> bookVos = new ArrayList<>();
        List<Book> books = bookMapper.getAllBook();
        if (books == null){
            return null;
        }
        for (Book book : books) {
            BookVo bookVo = new BookVo(book);
            bookVos.add(bookVo);
        }
        return bookVos;
    }


    /**
     * 获取所有类型
     * @return
     */
    public List<BookTypeVo> getAllType(){
        List<BookTypeVo> bookTypeVos = new ArrayList<>();
        List<BookType> allType = bookMapper.getAllType();
        if (allType == null){
            return null;
        }
        for (BookType bookTypeVo : allType) {
            BookTypeVo bookTypeVo1 = new BookTypeVo(bookTypeVo);
            bookTypeVos.add(bookTypeVo1);
        }
        return bookTypeVos;
    }

    /**
     * 根据类型获取书籍
     * @param id
     * @return
     */
    public List<BookVo> getBookByType(int id,int page,int num){
        List<BookVo> bookVos = new ArrayList<>();
        if (page < 1){
            return null;
        }
        List<Book> bookByType = bookMapper.getBookByType(id,(page-1)*num,num);
        if (bookByType.size() < 1){
            return null;
        }
        for (Book book : bookByType) {
            BookVo bookVo = new BookVo(book);
            bookVos.add(bookVo);
        }
        return bookVos;
    }


    /**
     * 根据书的id获取书籍的信息
     * @param id
     * @return
     */
    public BookVo getBookMessage(int id){
        Book book = bookMapper.getBookMessage(id);
        if (book == null){
            return null;
        }
        BookVo bookVo = new BookVo(book);
        return bookVo;
    }


    /**
     * 修改图书状态
     * @param bookId
     * @return
     */
    public boolean changeBookState(int bookId,int state){
        int i = bookMapper.changeBookState(bookId,state);
        return i > 0;
    }

    /**
     * 书籍 分页
     * @param page
     * @param num
     * @return
     */
    public List<BookVo> getAllBookByPage(int page, int num) {
        if(page < 1){
            return null;
        }
        List<BookVo> allBookByPage = bookMapper.getAllBookByPage((page - 1) * num, num);
        if (allBookByPage.size() == 0){
            return null;
        }
        return allBookByPage;
    }


    /**
     * 根据作者或书的名称查询
     * @param author
     * @param name
     * @return
     */
    public List<BookVo> getBookByTypeOrByName(String author,String name){
        List<BookVo> voList = new ArrayList<>();
        List<Book> bookByName = bookMapper.getBookByNameOrAuthor(author, name);
        for (Book book : bookByName) {
            BookVo bookVo = new BookVo(book);
            voList.add(bookVo);
        }
        return voList;
    }
}